﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Security;
using AbstractClass;
using AbstractClass.Collections;
using AbstractClass.Collections.Generic;
using AbstractClass.NPerf;
using AbstractClass.Runtime.Caching;
using AbstractClass.Threading.Tasks;

namespace QuasarDemo
{
    internal class Program
    {
        private static readonly NPerfTestRunner PerfTestRunner = new NPerfTestRunner();
        private static readonly TestFactory Tester = new TestFactory(PerfTestRunner);
        private static readonly ResultTracer Tracer = new ResultTracer(Console.Out);

        private static void Main()
        {
            string key = "m";
            ConsoleMenu menu = CreateMenu();

            try
            {
                Console.SetWindowSize(Console.LargestWindowWidth - 50, Console.LargestWindowHeight - 10);
                PrintHeader();
                Console.WriteLine(@"Press any key to start the test...");
                Console.ReadKey(true);
                Console.Clear();

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(@"*********************************************************");
                Console.WriteLine(@"Initiating Performance Test sequence for Quasar Library");
                Console.WriteLine(@"
");

                Tracer.Attach(PerfTestRunner, false);

                while (!"q".Equals(key))
                {
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Magenta;

                    if ("m".Equals(key))
                        menu.Show();

                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine(@"Press q to exit | Press m for menu");
                    key = Console.ReadLine();
                    if ("m".Equals(key))
                        Console.Clear();
                }
            }

                #region Handled Exception

            catch (TargetInvocationException ex)
            {
                var knownExceptions = new List<Type>
                                          {
                                              typeof (ArgumentException),
                                              typeof (ArgumentNullException),
                                              typeof (ArgumentOutOfRangeException),
                                              typeof (ArgumentNullOrEmptyException),
                                              typeof (ArithmeticException),
                                              typeof (BinaryNumberFormatException),
                                              typeof (DirectoryNotFoundException),
                                              typeof (DivideByZeroException),
                                              typeof (FileNotFoundException),
                                              typeof (FormatException),
                                              typeof (InvalidCastException),
                                              typeof (InvalidOperationException),
                                              typeof (InvalidJobOperationException),
                                              typeof (KeyNotFoundException),
                                              typeof (MatrixSizeMismatchException),
                                              typeof (NoEvictionPolicyException),
                                              typeof (NotSupportedException),
                                              typeof (NullReferenceException),
                                              typeof (PocketOverflowException),
                                              typeof (SerializationException),
                                              typeof (SecurityException),
                                              typeof (UnauthorizedAccessException),
                                              typeof (Win32Exception)
                                          };
                Exception innerException = ex.InnerException;

                if (knownExceptions.Contains(innerException.GetType()))
                    PrintHandledErrorMessage(innerException);
                else
                    PrintUnhandledErrorMessage(innerException);
            }

                #endregion

                #region Unhandled Exception Possible Bug

            catch (Exception ex)
            {
                PrintUnhandledErrorMessage(ex);
            }

            #endregion

            Console.WriteLine();
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(@"****************************************************");
            Console.WriteLine(@"Performance Test for Quasar Library has been terminated");
            Console.ResetColor();
            Console.WriteLine(@"
Happy Coding
");
            Console.WriteLine(@"If you found a Bug please report it to http://quasar.codeplex.com/");
            Console.WriteLine(@"
Press any key to exit...");
            Console.ReadKey(true);
        }

        private static ConsoleMenu CreateMenu()
        {
            var menu = new ConsoleMenu(true);

            menu.AddSeparator("Generic Collection Tests");

            menu.Add("Binary Search Tree", Tester.CreateTest("BSTTest"));
            menu.Add("Binary Tree", Tester.CreateTest("BTTest"));
            menu.Add("AVL Tree", Tester.CreateTest("AVLTest"));
            menu.Add("Generic Tree", Tester.CreateTest("TreeTest"));
            menu.Add("Pocket", Tester.CreateTest("BagTest"));
            menu.Add("Graph", Tester.CreateTest("GraphTest"));

            menu.AddSeparator("Cache Test");

            menu.Add("Cache", Tester.CreateTest("CacheTest"));

            menu.AddSeparator("Specialized Collection Test");

            menu.Add("Conditions", Tester.CreateTest("ConditionsTest"));

            menu.AddSeparator();

            menu.Add("Extension Methods", Tester.CreateTest("ExtensionTest"));

            return menu;
        }

        private static void PrintHandledErrorMessage(Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(@"
");
            Console.WriteLine(@"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Console.WriteLine(@"Exception Detected");
            Console.WriteLine(ex);
            Console.ForegroundColor = ConsoleColor.Yellow;
        }

        private static void PrintUnhandledErrorMessage(Exception ex)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.Beep(1327, 500);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(@"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(@"========= 	BUG FOUND	 =========");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(@"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(
                @"Please Report this Bug to http://quasar.codeplex.com/ with the following stack trace and steps to reproduce");
            PrintHandledErrorMessage(ex);
        }

        private static void PrintHeader()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(@"*************************************************************************");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*				QUASAR Library				*");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*		 Copyright (c) 2009-2011 Anindya Chatterjee 		*");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*  Licensed under the Apache License, Version 2.0 (the ""License"");	*");
            Console.WriteLine(@"*  you may not use this file except in compliance with the License.	*");
            Console.WriteLine(@"*  You may obtain a copy of the License at				*");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*		 http://www.apache.org/licenses/LICENSE-2.0 		*");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*  Unless required by applicable law or agreed to in writing, software	*");
            Console.WriteLine(@"*  distributed under the License is distributed on an ""AS IS"" BASIS,	*");
            Console.WriteLine(@"*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 	*");
            Console.WriteLine(@"*  implied.								*");
            Console.WriteLine(@"*  See the License for the specific language governing permissions and	*");
            Console.WriteLine(@"*  limitations under the License.					*");
            Console.WriteLine(@"*									*");
            Console.WriteLine(@"*************************************************************************");
            Console.WriteLine("");
            Console.WriteLine("");
        }
    }
}